package cn.lyjuan.android.mylibrary.view.recycler;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import cn.lyjuan.android.mylibrary.R;

/**
 * 设置属性<br />
 * 得用 {@code cache}缓存布局中设置的属性，在以后添加的ItemDecoration子类中设置cache
 * Created by chad on 07/11/2017.
 */
public abstract class AbstractItemDecoration extends RecyclerView.ItemDecoration
{
    private static final String TAG = AbstractItemDecoration.class.getSimpleName();

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    public Cache cache;

    /**
     * 设置属性
     *
     * @param attrs
     */
    public static Cache cacheAttrs(Context context, AttributeSet attrs)
    {
        if (null == attrs)
            return new Cache();

        Cache cache = new Cache();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RecyclerListView);

        cache.horizontalDivider = ta.getDrawable(R.styleable.RecyclerListView_rcy_horizontalDivider);
        cache.horizontalDividerWidth = (int) ta.getDimension(R.styleable.RecyclerListView_rcy_horizontalDividerWidth, 0);

        cache.verticalDivider = ta.getDrawable(R.styleable.RecyclerListView_rcy_verticalDivider);
        cache.verticalDividerHeight = (int) ta.getDimension(R.styleable.RecyclerListView_rcy_verticalDividerHeight, 0);

        cache.orientation = ta.getInt(R.styleable.RecyclerListView_rcy_orientation, VERTICAL_LIST);
        if (cache.orientation != HORIZONTAL_LIST)// 防止无效值
            cache.orientation = VERTICAL_LIST;

        return cache;
    }

    public static class Cache
    {
        public Drawable horizontalDivider;
        public Drawable verticalDivider;
        public int verticalDividerHeight = 0;
        public int horizontalDividerWidth = 0;
        public int orientation = VERTICAL_LIST;
    }
}
